package com.example.controller.web;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.example.model.api.Result;
import com.example.utils.Cookies;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
public class AdminController {
    @Value("${defaultClientId}")
    private String defaultClientId;

    @Value("${defaultClientSecret}")
    private String defaultClientSecret;

//    @Value("${defaultClientPrimaryId}")
//    private Long defaultClientPrimaryId;
//
//    @Value("${defaultStoreId}")
//    private Long defaultStoreId;
//
//    @Value("${defaultTenantId}")
//    private Long defaultTenantId;

    @Autowired
    private Environment environment;
    @Autowired
    private JdbcOperations jdbcTemplate;

    @GetMapping("index-callback")
    @ResponseBody
    public void indexcallback(HttpServletResponse response, @RequestParam Map<String, Object> params) throws IOException {
        String code = (String) params.get("code");
        JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate);
        RegisteredClient client = registeredClientRepository.findByClientId(defaultClientId);
        Set<String> redirectUris = client.getRedirectUris();
        Set<String> postLogoutRedirectUris = client.getPostLogoutRedirectUris();

        HashMap<String, Object> objectHashMap = new HashMap<>();
        objectHashMap.put("grant_type", AuthorizationGrantType.AUTHORIZATION_CODE.getValue());
        objectHashMap.put("code", code);
        objectHashMap.put("redirect_uri", ListUtil.toList(redirectUris).get(0));
        String body = HttpRequest.post(environment.getProperty("spring.security.oauth2.resourceserver.jwt.issuer-uri") + "/oauth2/token")
                .basicAuth(defaultClientId, defaultClientSecret)
                .form(objectHashMap)
                .execute().body();
        Map map = JSONUtil.toBean(body, Map.class);
        Cookies.sendRedirectCookies(response, (String) map.get("access_token"), ListUtil.toList(postLogoutRedirectUris).get(0));
    }

    @RequestMapping("home")
    public String home(){
        return "home";
    }
    @RequestMapping("login")
    public String login(){
        return "login";
    }

    @GetMapping("hello")
    @ResponseBody
    public Mono<String> gethello() {
        return Mono.just("Hello, me!");
    }
}
